package com.gzc.just.play.last.war.common.message.handler;

import com.gzc.just.play.last.war.common.service.GameService;
import com.gzc.just.play.last.war.common.quest.QuestManager;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Quest message handler
 */
@Component
public class QuestHandler implements MessageHandlerInterface {

    private static final Logger log = LoggerFactory.getLogger(QuestHandler.class);

    private final GameService gameService;
    private final QuestManager questManager;
    private final ObjectMapper objectMapper;
    
    public QuestHandler(GameService gameService, QuestManager questManager, ObjectMapper objectMapper) {
        this.gameService = gameService;
        this.questManager = questManager;
        this.objectMapper = objectMapper;
    }

    @Override
    public void handle(Long playerId, Object messageData) {
        try {
            // Parse quest data
            Map<String, Object> questData = objectMapper.convertValue(messageData, Map.class);
            String action = (String) questData.get("action");
            
            switch (action) {
                case "get_quests":
                    handleGetQuests(playerId);
                    break;
                case "accept_quest":
                    handleAcceptQuest(playerId, questData);
                    break;
                case "complete_quest":
                    handleCompleteQuest(playerId, questData);
                    break;
                case "abandon_quest":
                    handleAbandonQuest(playerId, questData);
                    break;
                default:
                    log.warn("Unknown quest action: {}", action);
            }
        } catch (Exception e) {
            log.error("Error processing quest message for player: {}", playerId, e);
        }
    }
    
    /**
     * Handle get quests
     */
    private void handleGetQuests(Long playerId) {
        // Simple implementation
        log.debug("Player {} requested quest list", playerId);
    }
    
    /**
     * Handle accept quest
     */
    private void handleAcceptQuest(Long playerId, Map<String, Object> questData) {
        // Simple implementation
        log.debug("Player {} accepted quest", playerId);
    }
    
    /**
     * Handle complete quest
     */
    private void handleCompleteQuest(Long playerId, Map<String, Object> questData) {
        // Simple implementation
        log.debug("Player {} completed quest", playerId);
    }
    
    /**
     * Handle abandon quest
     */
    private void handleAbandonQuest(Long playerId, Map<String, Object> questData) {
        // Simple implementation
        log.debug("Player {} abandoned quest", playerId);
    }
}